package com.h2cloud;

import java.util.*;
import java.util.concurrent.*;

public class testThreadPool {
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(10);
        List<Future<?>> futureList = new ArrayList<>();
        Map<Integer, Integer> approverMap = new HashMap<>();
        long l = System.currentTimeMillis();
        for (int i = 0; i < 15; i++) {
            int var = i + 1;

            Callable<String> r = () -> {
                Random random = new Random();
                int i1 = random.nextInt(11);
                if (i1 < 50) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                approverMap.put(var, var);
                return "thread:" + var;
            };

            Future<?> submit = es.submit(r);
            futureList.add(submit);
        }

        futureList.forEach(future -> {
            try {
                long l1 = System.currentTimeMillis();
                System.out.println(future.get() + "  cast:" + (System.currentTimeMillis() - l1));
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });

        System.out.println("size:" + approverMap.keySet().size());
        approverMap.keySet().forEach(
                k -> {
                    System.out.println(approverMap.get(k));
                }
        );
        System.out.println(System.currentTimeMillis() - l);
        System.out.println("main end");

    }
}
